package com.vive;

import com.vive.model.AuditData;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.io.IOUtils;
import org.apache.wicket.request.resource.ByteArrayResource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AuditViewPage extends WebPage {

    // 模拟审批数据
    private List<AuditData> auditList = new ArrayList<AuditData>();

    public AuditViewPage(PageParameters parameters) {
        initData(); // 初始化模拟数据

        // 创建审批列表
        ListView<AuditData> auditListView = new ListView<AuditData>("auditList", auditList) {
            @Override
            protected void populateItem(ListItem<AuditData> item) {
                AuditData auditData = item.getModelObject();
                // 添加缺失的id组件
                item.add(new Label("id", String.valueOf(auditData.getId())));
                item.add(new Label("username", auditData.getUsername()));
                item.add(new Label("reason", auditData.getReason()));
                item.add(new Label("startTime", auditData.getStartTime()));
                item.add(new Label("endTime", auditData.getEndTime()));
                item.add(new Label("departureAddr", auditData.getDepartureAddr()));
                item.add(new Label("destinationAddr", auditData.getDestinationAddr()));

                // 创建图片资源
//                Image licenseImage = new Image("licenseImage", createImageResource(auditData.getImgUrl()));
                Image licenseImage = new Image("licenseImage", createImageResource("D:\\vistaLee\\Pictures\\b.png"));
                item.add(licenseImage);
            }
        };

        add(auditListView);
    }

    // 创建图片资源的方法 (Wicket 3方式)
    private ByteArrayResource createImageResource(String imagePath) {
        try {
            File imageFile = new File(imagePath);
            if (imageFile.exists()) {
                FileInputStream fis = new FileInputStream(imageFile);
                byte[] imageData = IOUtils.toByteArray(fis);
                fis.close();

                String mimeType = "image/jpeg";
                if (imagePath.toLowerCase().endsWith(".png")) {
                    mimeType = "image/png";
                } else if (imagePath.toLowerCase().endsWith(".gif")) {
                    mimeType = "image/gif";
                }

                return new ByteArrayResource(mimeType, imageData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ByteArrayResource("image/png", new byte[0]);
    }

    // 初始化模拟数据
    private void initData() {
        AuditData data1 = new AuditData();
        data1.setId(1);
        data1.setUsername("张三");
        data1.setReason("出差");
        data1.setStartTime("2023-10-01 09:00");
        data1.setEndTime("2023-10-01 18:00");
        data1.setDepartureAddr("北京");
        data1.setDestinationAddr("上海");
        // 本地文件路径
        data1.setImgUrl("D:\\vistaLee\\Pictures\\a.png");

        auditList.add(data1);
    }
}
